<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="/webjars/bootstrap/3.3.7/css/bootstrap.min.css">
    <script type="text/javascript" src="/webjars/jquery/3.4.1/jquery.js"></script>
    <script type="text/javascript" src="/webjars/bootstrap/3.3.7/js/bootstrap.min.js"></script>


    <script type="text/javascript" src="/webjars/vue/2.6.10/dist/vue.js"></script>
    <script type="text/javascript" src="/webjars/axios/0.19.0/dist/axios.js"></script>
    <style>

    </style>
</head>
<body>

<div id="vuebox">
    <div class="container">
        <div class="row" style="margin-top: 50px;">
            <h1 style="text-align: center">空气质量检测信息库</h1>
            <div class="col-md-6">

                <form class="form-inline" role="form" style="padding-left: 250px;">
                    <div class="form-group" style="padding-left: 20px;">
                        <label>按区域查询:</label>
                        <select class="form-control" v-model="districtId"><!--骆驼命名法-->
                            <option value="0">不限</option>
                            <option v-for="item in districtList.data" :value="item.id">{{item.name}}</option>
                        </select>
                    </div>
                    <div class="form-group" style="padding-left: 20px;">
                        <button type="button" class="btn btn-primary" @click="selectIndex(1)">查询</button>
                    </div>

                </form>
            </div>
            <div class="col-md-4 text-right">
                <a @click.stop="showModal()" class="btn btn-primary">添加空气质量信息</a>
            </div>
        </div>

        <br/>
        <table class="table table-striped">
            <thead>
            <tr>
                <th>序号</th>
                <th>区域</th>
                <th>检测时间</th>
                <th>pm10数据</th>
                <th>pm2.5数据</th>
                <th>监测站</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <tr v-for="item in indexList.data.list">
                <td>{{item.districtid}}</td>
                <td></td>
                <td>{{item.monitortime}}</td>
                <td>{{item.pm10}}</td>
                <td>{{item.pm25}}</td>
                <td>{{item.monitoringstation}}</td>
                <td>
                    <button class="btn btn-success" @click="to_update_entry(item.id)">修改</button>
                    <button class="btn btn-danger" @click="deleteIndex(item.id)">删除</button>
                </td>
            </tr>
            </tbody>
        </table>
        <br/>


        <div class="text-center">
            <ul class="pagination">
                <li v-show="indexList.data.hasPreviousPage">
                    <a href="#" @click.stop="selectIndex(indexList.data.perPage)">&laquo;</a>
                </li>
                <li :class="{active:indexList.data.pageNum==i}" v-for="i of indexList.data.navigatepageNums">
                    <a href="#" @click.stop="selectIndex(i)">{{i}}</a>
                </li>
                <li v-show="indexList.data.hasNextPage">
                    <a href="#" @click.stop="selectIndex(indexList.data.nextPage)">&raquo;</a>
                </li>
            </ul>
        </div>
    </div>

    <!--添加模态框-->
    <!--模态框-->
    <div class="modal" id="mymodal">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" type="button" @click="hideModal()">&times;</button>
                    <div class="modal-title">
                        添加空气质量检测信息库信息
                    </div>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" role="form">

                        <input type="text" v-model="new_entry.id" id="index_id">

                        <div class="form-group">
                            <label for="districtId" class="col-sm-2 control-label">区域</label>
                            <div class="col-sm-10">
                                <select id="districtId" class="form-control" v-model="new_entry.districtid">
                                    <option value="0">全部</option>
                                    <option v-for="item in districtList.data" :value="item.id">{{item.name}}</option>
                                </select>
                            </div>
                        </div>

                       <!-- <div class="form-group">
                            <label for="monitortime" class="col-sm-2 control-label">区域</label>
                            <div class="col-sm-10">
                                <input v-model="new_entry.monitortime" id="monitortime" type="text" class="form-control" placeholder="请输入监测时间">
                            </div>
                        </div>
-->

                        <div class="form-group">
                            <label for="monitortime" class="col-sm-2 control-label">监测时间</label>
                            <div class="col-sm-10">
                                <input v-model="new_entry.monitortime" id="monitortime" type="text" class="form-control" placeholder="请输入监测时间">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="pm10" class="col-sm-2 control-label">pm10值</label>
                            <div class="col-sm-10">
                                <input v-model="new_entry.pm10" id="pm10" type="text" class="form-control" placeholder="请输入pm10值"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="pm25" class="col-sm-2 control-label">pm2.5值</label>
                            <div class="col-sm-10">
                                <input v-model="new_entry.pm25" id="pm25" type="text" class="form-control" placeholder="请输入pm2.5值">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="monitoringstation" class="col-sm-2 control-label">监测站</label>
                            <div class="col-sm-10">
                                <input v-model="new_entry.monitoringstation" id="monitoringstation" type="text" class="form-control" placeholder="请输入监测站">
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button class="btn btn-default" @click="hideModal()">关闭</button>
                    <button class="btn btn-primary" @click="addIndex()">保存</button>
                </div>
            </div>
        </div>
    </div>


</div>

</body>
</html>
<script type="text/javascript">
    var vm = new Vue({
        el: "#vuebox",
        data: {
            districtList: {},
            districtId: "0",
            indexList: {},
            delResult:{},
            new_entry:{id:"",districtid:"0",monitortime:"",pm10:"",pm25:"",monitoringstation:""}
        }, methods: {
            selectIndex: function (pageNum) {
                axios.get("/selectPageInfo",{params:{districtId:this.districtId,pageNum:pageNum}})
                    .then(res=>{
                        this.indexList=res.data;
                    this.pageNum=pageNum;
                    });
            },
            deleteIndex:function (id) {
                axios.get("/deleteIndex",{params:{id:id}}).then(
                    res=>{this.delResult=res.data;
                    if (this.delResult.result='SUCCESS'){
                        this.selectIndex(this.pageNum);
                        alert("删除成功!");
                    }else{
                        alert(this.delResult.message);
                    }
                    })
            },
            showModal:function(){
                $("#mymodal").modal("show");
            },
            hideModal:function(){
                $("#mymodal").modal("hide");
                //清除模态框内容
                this.clearForm();
            },
            clearForm:function () {
                $("#index_id").val("");
                $("#districtId").val(0);
                $("#monitortime").val("");
                $("#pm10").val("");
                $("#pm25").val("");
                $("#monitoringstation").val("");
            },
            addIndex:function () {
                axios.post("/addIndex",this.new_entry).then(res=>{
                    this.delResult=res.data;
                    if(this.delResult.result="SUCCESS"){
                        //增加成功默认到第一页
                        this.selectIndex(this.pageNum);
                        //增加成功关闭模态框
                        this.hideModal();
                    }else{
                        alert(this.delResult.message);
                    }
                })
            },
            to_update_entry:function (id) {
                axios.get("/IndexById",{params:{id:id}}).then(res=>{
                    this.new_entry=res.data.data;
                    this.showModal();
                })
            }
        },

        mounted() {
            axios.get("/selectAll").then(res => {
                this.districtList = res.data
            });
            this.selectIndex(1);

        }
    })
</script>